/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.models.beans;

import com.models.entities.TblSubject;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless
public class SubjectSessionBean {

    @PersistenceContext(unitName = "cungonbaiPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    public boolean InsertSubject(TblSubject subject) {
        try {
            persist(subject);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public List<TblSubject> loadAllSubject() {
        Query query = em.createNamedQuery("TblSubject.findAll");
        return query.getResultList();
    }

    public boolean deleteSubject(int subjectId) {
        TblSubject subject = em.find(TblSubject.class, subjectId);
        if (subject != null) {
            em.remove(subject);
            return true;
        } else {
            return false;
        }
    }
    
    public TblSubject findSubject(String subjectName){
        Query query = em.createQuery("SELECT t FROM TblSubject t WHERE t.subjectName LIKE :subjectName");
        query.setParameter("subjectName", subjectName);
        TblSubject subject = new TblSubject();
        try {
            subject = (TblSubject) query.getSingleResult();
            return subject;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
